package Dao.Daoimpl;

import Dao.UserMsg;
import Pojo.Msgpojo;
import Pojo.UserMsgpojo;
import utils.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserMsgimpl implements UserMsg {
    @Override
    public boolean Addusermsg(UserMsgpojo um) {
        try {
            Connection getconnection = jdbc.getconnection();
            String sql="insert into usermsg (name,checkstatus,state,sex,age) value (?,?,?,?,?)";
            PreparedStatement psm=null;
            psm=getconnection.prepareStatement(sql);
                psm.setString(1,um.getName());
                psm.setString(2,um.getCheckstatus());
                psm.setString(3,um.getState());
                psm.setString(4,um.getSex());
                psm.setInt(5,um.getAge());
                     int i = psm.executeUpdate();
//                     System.out.println(i);
                     jdbc.close(getconnection);
                     if (i==1){
                         return true;
                     }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    @Override
    public List<Msgpojo> GetMsg() {
        List<Msgpojo> allmsg=new ArrayList<Msgpojo>();
        Connection conn=null;
        try {
            conn = jdbc.getconnection();
            String sql ="select * from msg";
            PreparedStatement pre=null;
            pre=conn.prepareStatement(sql);
            ResultSet res = pre.executeQuery();
                while (res.next()){
                    int mid =res.getInt(1);
                String header=res.getString(2);
                String msgtype=res.getString(3);
                String msgmember=res.getString(4);
                String msgtext=res.getString(5);
                Date msgtime=res.getDate(6);
                Msgpojo mj=new Msgpojo(mid,header,msgtype,msgmember,msgtext,msgtime);
                allmsg.add(mj);
             }
                jdbc.close(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return allmsg;
    }
}
